﻿using Admin.NET.Core.Service;
using Admin.NET.Application.Const;
using Admin.NET.Application.Entity;
using Microsoft.AspNetCore.Http;
namespace Admin.NET.Application;
/// <summary>
/// 盲板信息服务
/// </summary>
[ApiDescriptionSettings(ApplicationConst.GroupName, Order = 100)]
public class BoardBaseService : IDynamicApiController, ITransient
{
    private readonly SqlSugarRepository<BoardBase> _rep;
    public BoardBaseService(SqlSugarRepository<BoardBase> rep)
    {
        _rep = rep;
    }

    /// <summary>
    /// 分页查询盲板信息
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    [HttpPost]
    [ApiDescriptionSettings(Name = "Page")]
    public async Task<SqlSugarPagedList<BoardBaseOutput>> Page(BoardBaseInput input)
    {
        var query = _rep.AsQueryable()
            .WhereIF(!string.IsNullOrWhiteSpace(input.SearchKey), u =>
                u.BoardNo.Contains(input.SearchKey.Trim())
                || u.BoardPlace.Contains(input.SearchKey.Trim())
                || u.BoardState.Contains(input.SearchKey.Trim())
                || u.Note.Contains(input.SearchKey.Trim())
            )
            .WhereIF(!string.IsNullOrWhiteSpace(input.BoardNo), u => u.BoardNo.Contains(input.BoardNo.Trim()))
            .WhereIF(input.BoardDevice>0, u => u.BoardDevice == input.BoardDevice)
            .WhereIF(!string.IsNullOrWhiteSpace(input.BoardPlace), u => u.BoardPlace.Contains(input.BoardPlace.Trim()))
            .WhereIF(!string.IsNullOrWhiteSpace(input.BoardState), u => u.BoardState.Contains(input.BoardState.Trim()))
            .WhereIF(!string.IsNullOrWhiteSpace(input.Note), u => u.Note.Contains(input.Note.Trim()))
            //处理外键和TreeSelector相关字段的连接
            //.LeftJoin<BoardDevice>((u, boarddevice) => u.BoardDevice == boarddevice.Id )
            .OrderBy(u => u.CreateTime)
            .Select(u  => new BoardBaseOutput
            {
                Id = u.Id,
                BoardNo = u.BoardNo,
                RouteNo = u.RouteNo,
                BoardDevice = u.BoardDevice, 
                BoardDeviceDeviceName = u.Device.DeviceName,
                BoardPlace = u.BoardPlace,
                BoardState = u.BoardState,
                Note = u.Note,
                CreateOrgId = u.CreateOrgId,
                CreateOrgName = u.CreateOrgName,
                CreateTime = u.CreateTime,
                UpdateTime = u.UpdateTime,
                CreateUserId = u.CreateUserId,
                CreateUserName = u.CreateUserName,
                UpdateUserId = u.UpdateUserId,
                UpdateUserName = u.UpdateUserName,
                IsDelete = u.IsDelete,
            });
        return await query.OrderBuilder(input).ToPagedListAsync(input.Page, input.PageSize);
    }

    /// <summary>
    /// 增加盲板信息
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    [HttpPost]
    [ApiDescriptionSettings(Name = "Add")]
    public async Task<long> Add(AddBoardBaseInput input)
    {
        var entity = input.Adapt<BoardBase>();
        await _rep.InsertAsync(entity);
        return entity.Id;
    }

    /// <summary>
    /// 删除盲板信息
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    [HttpPost]
    [ApiDescriptionSettings(Name = "Delete")]
    public async Task Delete(DeleteBoardBaseInput input)
    {
        var entity = await _rep.GetFirstAsync(u => u.Id == input.Id) ?? throw Oops.Oh(ErrorCodeEnum.D1002);
        await _rep.FakeDeleteAsync(entity);   //假删除
        //await _rep.DeleteAsync(entity);   //真删除
    }

    /// <summary>
    /// 更新盲板信息
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    [HttpPost]
    [ApiDescriptionSettings(Name = "Update")]
    public async Task Update(UpdateBoardBaseInput input)
    {
        var entity = input.Adapt<BoardBase>();
        await _rep.AsUpdateable(entity).IgnoreColumns(ignoreAllNullColumns: true).ExecuteCommandAsync();
    }

    /// <summary>
    /// 获取盲板信息
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    [HttpGet]
    [ApiDescriptionSettings(Name = "Detail")]
    public async Task<BoardBase> Detail([FromQuery] QueryByIdBoardBaseInput input)
    {
        return await _rep.GetFirstAsync(u => u.Id == input.Id);
    }

    /// <summary>
    /// 获取盲板信息列表
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    [HttpGet]
    [ApiDescriptionSettings(Name = "List")]
    public async Task<List<BoardBaseOutput>> List([FromQuery] BoardBaseInput input)
    {
        return await _rep.AsQueryable().Select<BoardBaseOutput>().ToListAsync();
    }

    /// <summary>
    /// 获取盲板所属设备列表
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    [ApiDescriptionSettings(Name = "BoardDeviceBoardDeviceDropdown"), HttpGet]
    public async Task<dynamic> BoardDeviceBoardDeviceDropdown()
    {
        return await _rep.Context.Queryable<BoardDevice>()
                .Select(u => new
                {
                    Label = u.DeviceName,
                    Value = u.Id
                }
                ).ToListAsync();
    }
    public  BoardBase getBoard(string boardNo)
    {
        return  _rep.GetFirstAsync(u => u.BoardNo == boardNo).Result;
    }

    /// <summary>
    /// 分页查询接收的SocketDatat
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    [HttpPost]
    [ApiDescriptionSettings(Name = "getRouteBoards")]
    public async Task<dynamic> getRouteBoards(List<string> lst)
    {
        return await _rep.AsQueryable()
            .Where(u => lst.Contains(u.RouteNo))
             .Select(u => new
             {
                 id = u.BoardNo,
                 state = u.BoardState
             }
                ).ToListAsync();
    }

    /// <summary>
    /// 更新盲板信息
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>

    public  void UpdateData(BoardBase entity)
    {
        _rep.AsUpdateable(entity).IgnoreColumns(ignoreAllNullColumns: true).ExecuteCommand();
    }
}

